/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.unindra.raport.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;

import edu.unindra.raport.dao.entity.Siswa;
import edu.unindra.raport.dao.interf.SiswaInterf;
import edu.unindra.raport.dao.koneksi.DatabaseUtilities;

/**
 * 
 * @author fendi
 */
public class SiswaImpl implements SiswaInterf {

	@Override
	public int insert(Siswa si) throws SQLException {
		int result = -1;
		PreparedStatement ps = null;
		try {
			ps = DatabaseUtilities.getInstance().getConnection()
					.prepareStatement("insert into siswa values (?,?,?,?,?,?)");

			ps.setString(1, si.getNis());
			ps.setString(2, si.getNama());
			ps.setString(3, si.getTeTgl());
			ps.setString(4, si.getTaTgl());
			ps.setInt(5, si.getKls());
			ps.setInt(6, si.getThnAjaran());

			result = ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ps.close();
		}
		return result;
	}

	@Override
	public Siswa getSiswa() throws SQLException {
		Siswa si = new Siswa();

		Statement st = null;
		ResultSet rs = null;
		try {
			st = DatabaseUtilities.getInstance().getConnection()
					.createStatement();
			rs = st.executeQuery("select * from siswa");
			while (rs.next()) {
				si.setNis(rs.getString("NIS"));
				si.setNama(rs.getString("NAMA_SISWA"));
				si.setTeTgl(rs.getString("TEMPAT_LAHIR"));
				si.setTaTgl(rs.getString("TGL_LAHIR"));
				si.setKls(Integer.valueOf(rs.getString("KELAS")));
				si.setThnAjaran(Integer.valueOf(rs.getString("TAHUN_AJARAN")));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			st.close();
			rs.close();
		}
		return si;
	}

	@Override
	public List<Siswa> getSiswaAll() throws SQLException {
		List<Siswa> sis = new LinkedList<Siswa>();
		Siswa si = null;

		Statement st = null;
		ResultSet rs = null;
		try {
			st = DatabaseUtilities.getInstance().getConnection()
					.createStatement();
			rs = st.executeQuery("select * from siswa");
			while (rs.next()) {
				si = new Siswa();

				si.setNis(rs.getString("NIS"));
				si.setNama(rs.getString("NAMA_SISWA"));
				si.setTeTgl(rs.getString("TEMPAT_LAHIR"));
				si.setTaTgl(rs.getString("TGL_LAHIR"));
				si.setKls(Integer.valueOf(rs.getString("KELAS")));
				si.setThnAjaran(Integer.valueOf(rs.getString("TAHUN_AJARAN")));

				sis.add(si);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			st.close();
			rs.close();
		}
		return sis;
	}

	@Override
	public int delete(String id) throws SQLException {
		int result = -1;
		PreparedStatement ps = null;
		try {
			ps = DatabaseUtilities.getInstance().getConnection()
					.prepareStatement("delete from siswa where NIS = ?");

			ps.setString(1, id);
			result = ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ps.close();
		}

		return result;
	}

	@Override
	public int update(Siswa si) throws SQLException {
		int result = -1;

		PreparedStatement ps = null;
		try {
			ps = DatabaseUtilities
					.getInstance()
					.getConnection()
					.prepareStatement(
							"update siswa set NAMA_SISWA = ?,TEMPAT_LAHIR = ?,TGL_LAHIR = ?,KELAS = ?,TAHUN_AJARAN = ? where NIS = ? ");

			ps.setString(1, si.getNama());
			ps.setString(2, si.getTeTgl());
			ps.setString(3, si.getTaTgl());
			ps.setInt(4, si.getKls());
			ps.setInt(5, si.getThnAjaran());
			ps.setString(6, si.getNis());

			result = ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ps.close();
		}
		return result;
	}
}
